package com.ninong.ker.admin.rest;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.ninong.ker.admin.service.LoginService;
import com.ninong.ker.admin.service.dto.UserDTO;
import com.ninong.ker.common.annotation.log.Log;
import com.ninong.ker.common.auth.JwtProperties;
import com.ninong.ker.common.response.CmdResponse;
import com.ninong.ker.common.tools.Constants;
import com.ninong.ker.common.tools.RedisUtils;
import com.ninong.ker.common.tools.enums.BusinessEnum;
import com.wf.captcha.ArithmeticCaptcha;

/**
 * 登录
 *
 * @author cmd
 */
@RestController
@RequestMapping("/cmd/v1/auth")
public class LoginResouces {

    @Autowired
    private LoginService loginService;

    @Autowired
    private JwtProperties properties;

    @Autowired
    private RedisUtils redisUtils;

    @Value("${login.login-code.expiration}")
    private long expiration;

    /**
     * 登录
     *
     * @param request
     * @param dto
     * @return
     * @throws Exception
     */
    @Log("登录")
    @PostMapping(value = "/login")
    public ResponseEntity login(HttpServletRequest request, @RequestBody UserDTO dto) throws Exception {
        return ResponseEntity.ok(loginService.login(dto));
    }


    /**
     * 合法菜单
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/menus")
    public ResponseEntity menus(HttpServletRequest request) {
        CmdResponse response = new CmdResponse();
        response.setData(loginService.listMenus());
        return ResponseEntity.ok(response);
    }

    /**
     * 用户信息
     *
     * @return
     */
    @GetMapping(value = "/info")
    public ResponseEntity info() {
        CmdResponse response = new CmdResponse();
        response.setData(loginService.listInfo());
        return ResponseEntity.ok(response);
    }

    /**
     * 在线用户
     *
     * @return
     */
    @PostMapping(value = "/online")
    public ResponseEntity online(@RequestBody UserDTO dto) {
        CmdResponse response = new CmdResponse();
        response.setData(loginService.listOnline(dto));
        return ResponseEntity.ok(response);
    }

    /**
     * 租户切换
     *
     * @return
     */
    @GetMapping(value = "/change/{id}")
    public ResponseEntity change(@PathVariable String id) {
        CmdResponse response = new CmdResponse();
        response.setData(loginService.changeTenant(id));
        return ResponseEntity.ok(response);
    }

    /**
     * 驱逐某个在线用户
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/expel")
    public ResponseEntity expel(@RequestBody String[] ids) {
        CmdResponse response = new CmdResponse();
        loginService.removeOnline(ids);
        response.setMessage(BusinessEnum.DELETE_SUCCESS.getMsg());
        return ResponseEntity.ok(response);
    }

    @GetMapping(value = "/code")
    public ResponseEntity<Object> getCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(111, 36);
        // 几位数运算，默认是两位
        captcha.setLen(2);
        // 获取运算的结果
        String result = captcha.text();
        String uuid = properties.getCode() + UUID.randomUUID().toString();
        // 保存
        redisUtils.set(uuid, result, expiration, TimeUnit.MINUTES);
        // 验证码信息
        @SuppressWarnings("serial")
        Map<String, Object> imgResult = new HashMap<String, Object>(2) {
            {
                put("img", captcha.toBase64());
                put("uuid", uuid);
            }
        };
        CmdResponse response = new CmdResponse();
        response.setData(imgResult);
        return ResponseEntity.ok(response);
    }

}
